package com.projectplace.GUI;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.GUI.Callback.CacheCallbackInterface.ResponseConversation;
import com.projectplace.GUI.Database.CacheHelper;
import com.projectplace.GUI.adapter.Conversation_Conversation_Adapter;
import com.projectplace.api.ConversationsApi;
import com.projectplace.api.objectResponses.Conversation.Conversation;

public class SingelConversation extends ListActivity{

	private Conversation_Conversation_Adapter conversationAdapter;
	private static Conversation conversation;
	private final String LOG_TAG = "SingelConversation";
	private ListActivity activity;
	private static String idOfClickedConversation="";
	private final String identifier="projectID";
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
		
		
		idOfClickedConversation = getIntent().getExtras().getString(identifier);
		activity = this;
	}
	
	public void onClick(View v){
		finish();
	}
	
	//Listens for user clicks, have to parse from long since that's what the adapter is returning
	public void onListItemClick(ListView l,View v,int position,long id){
		String idOfClickedProject = String.valueOf(id);
		Log.d(LOG_TAG, idOfClickedProject);
		//Intent intent = new Intent().setClass(this, ConversationComment.class);
		//intent.putExtra(identifier, idOfClickedProject);
		//startActivity(intent);
	}
	
	public void onStart(){
		super.onStart();
		
			CacheHelper.getMyConversation(activity, new Handler(), idOfClickedConversation, new ResponseConversation() {	
				public void onFail(Error err) {
					// TODO Auto-generated method stub
					Log.d(LOG_TAG, err.getMessage());	
				}
				
				public void onReceive(Conversation conv) {
					// TODO Auto-generated method stub
					if(conv != null){
						conversation = conv;
						conversationAdapter = new Conversation_Conversation_Adapter(activity,conversation);
						activity.setListAdapter(conversationAdapter);
					}
				}
			});
	}
	
	/*
	 * Metod takes you one step ahead in the gui from listing projectconversation to list a singel Conversation
	 */
	public void onResume(){
	super.onResume();
		ConversationsApi.getConversation(SingeltonPPClient.getInstance(), new Handler(), idOfClickedConversation , new com.projectplace.api.Callback.Callback.ResponseListenerConversation() {
			public void SERVER_ERROR(Error err) {
				// TODO Auto-generated method stub
				
			}
			
			public void BAD_TOKEN_ERROR(Error err) {
				// TODO Auto-generated method stub
				
			}
			
			public void onReceive(Conversation conv) {
				// TODO Auto-generated method stub
				if(conversationAdapter != null){
					Log.d(LOG_TAG, "Inte null");
					CacheHelper.saveMyConversation(activity, conv,idOfClickedConversation);
					conversation.setPosts(conv.getPosts());
					conversationAdapter.notifyDataSetChanged();
				}else{
					Log.d(LOG_TAG, "null");
					conversation = conv;
					conversationAdapter = new Conversation_Conversation_Adapter(activity,conversation);
					activity.setListAdapter(conversationAdapter);
				}
			}
		});
	}
	
	public void onPause(){
		super.onPause();
		Log.d(LOG_TAG, "Pausing!!");
	}
	
	public void onRestart(){
		super.onRestart();
		Log.d(LOG_TAG, "Restarting!!");
		//conversationAdapter = null;
		//conversation.clear();
	}
}
